import 'package:flutter/widgets.dart';
import 'package:flutter_module/base/model/constants.dart';
import 'package:flutter_module/base/util/sp_util.dart';
import 'package:flutter_module/util/language_res/str_res_keys.dart';
import 'package:get/get.dart';

//参考 from ： https://github.com/lilongweidev/study_language_theme.git
class FourController extends GetxController with WidgetsBindingObserver {
  final languageStr = SR.followerSystemLanguage.tr.obs;
  final themeStr = SR.followerSystemTheme.tr.obs;

  @override
  void onInit() {
    super.onInit();
    WidgetsBinding.instance.addObserver(this);
    getLanguage();
    getTheme();
  }

  @override
  onClose() {
    super.onClose();
    WidgetsBinding.instance.removeObserver(this);
  }

  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    super.didChangeAppLifecycleState(state);
    getLanguage();
  }

  @override
  void didChangePlatformBrightness() {
    super.didChangePlatformBrightness();
    getTheme();
  }

  //获取语言
  void getLanguage() {
    var language = SharedPreferencesU.getInstance().get(Constants.language);
    switch (language) {
      case 0:
        languageStr.value = SR.followerSystemLanguage.tr;
        break;
      case 1:
        languageStr.value = SR.simplifiedChinese.tr;
        break;
      case 2:
        languageStr.value = 'English';
        break;
      default:
        languageStr.value = SR.followerSystemLanguage.tr;
        break;
    }
  }

  //获取主题
  void getTheme() {
    var theme = SharedPreferencesU.getInstance().get(Constants.theme);
    switch (theme) {
      case 0:
        themeStr.value = SR.followerSystemTheme.tr;
        break;
      case 1:
        themeStr.value = SR.lightTheme.tr;
        break;
      case 3:
        themeStr.value = SR.greenTheme.tr;
        break;
      case 4:
        themeStr.value = SR.orangeTheme.tr;
        break;
      case 5:
        themeStr.value = SR.purpleTheme.tr;
        break;
      case 6:
        themeStr.value = SR.pinkTheme.tr;
        break;
      case 7:
        themeStr.value = SR.tealTheme.tr;
        break;
      default:
        themeStr.value = SR.followerSystemTheme.tr;
        break;
    }
  }
}
